local fengxin = fk.CreateSkill{
  name = "investiture__fengxin",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["investiture__fengxin"] = "奉心",
  [":investiture__fengxin"] = string.format(
    "限定技，当你扣减体力至0后，你可将一张%s【闪电】置入判定区，若你的判定区内有此牌，你结算濒死后不会死亡；此【闪电】不能移动至其他角色的区域。",
    '<font color="red">'..'♥Q'..'</font>'
  ),

  ["@@investiture__fengxin"] = "奉心",

  ["$investiture__fengxin"] = "人若是无心，如何？",
}

fengxin:addEffect(fk.HpChanged, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(fengxin.name) and player == target and player.hp <= 0 and data.num < 0
    and player:usedSkillTimes(fengxin.name, Player.HistoryGame) == 0
    and not table.find(player:getCardIds("j"), function (c) return Fk:getCardById(c).trueName == "lightning" end)
  end,
  on_use = function (self, event, target, player)
    local room = player.room
    local lightning = room:printCard("lightning", Card.Heart, 12)
    room:setCardMark(lightning, "@@investiture__fengxin", player.id)
    room:moveCardTo(lightning, Card.PlayerJudge, player, fk.ReasonPut, fengxin.name, nil, true, player)
  end,
})

fengxin:addEffect(fk.BeforeCardsMove, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return table.find(player.room.alive_players, function (p)
      return p:hasSkill(fengxin.name)
    end)
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      for i = #move.moveInfo, 1, -1 do
        if Fk:getCardById(move.moveInfo[i].cardId):getMark("@@investiture__fengxin") == player.id then
          if move.to ~= player and move.toArea <= Card.PlayerJudge then
            player.room:notifySkillInvoked(player, fengxin.name, "defensive")
            if move.moveInfo[i].fromArea == Card.Processing and move.moveReason == fk.ReasonPut and not move.skillName then
              move.to = player
            else
              table.remove(move.moveInfo, i)
            end
          end
        end
      end
    end
  end,
})

fengxin:addEffect(fk.AskForPeachesDone, {
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player.hp <= 0 and player.dying and table.find(player:getCardIds("j"), function (c)
      return Fk:getCardById(c):getMark("@@investiture__fengxin") == player.id
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:notifySkillInvoked(player, fengxin.name, "support")
    data.ignoreDeath = true
  end,
})

return fengxin